<?php
	include_once "../Database.php";
	class Exam{
		private $examcode;
		private $examdate;
		private $maxmarks;
		private $score;
		private $wrongAns;
		
		public function __construct()
		{
			$this->score=0;
			$this->wrongAns=0;
		}
		public function setExamCode($examcode)
		{
			$this->examcode=$examcode;
		}
		
		public function getExamCode()
		{
			return $this->examcode;
		}
		
		public function setExamDate($examdate)
		{
			$this->examdate=$examdate;
		}
		
		public function getExamDate()
		{
			return $this->examdate;
		}
		
		public function setMaxMarks($maxmarks)
		{
			$this->maxmarks=$maxmarks;
		}
		
		public function getMaxMarks()
		{
			return $this->maxmarks;
		}
		
		public function setScore($score)
		{
			$this->score=$score;
		}
		
		public function getScore()
		{
			return $this->score;
		}
		
		public function getWrongAns()
		{
			return $this->wrongAns;
		}
		
		public function createExam()
		{
			$db=new Database();
			$str="insert into exam(examcode,examdate,maxmarks,avail) values('".$this->examcode."','".$this->examdate."',".$this->maxmarks.",0)";
			$response=$db->query($str);
			echo "Exam creation ".$response;
		}
		
		public function activateExam()
		{
			$db=new Database();
			$str1="update exam set avail=0 where avail=1";
			$str2="update exam set avail=1 where examcode='".$this->examcode."'";
			$response1=$db->query($str1);
			$response2=$db->query($str2);
			if($response1=="Successful" && $response2=="Successful")
			{
				echo "Exam Activation Successful";
			}
			else
			{
				echo "Exam Activation Failed";
			}
		}
		
		public function examCodeList()
		{
			$db=new Database();
			$response=$db->selectQueryJson("select distinct examcode from exam");
		}
		public function result($uname,$eid)
		{
			$db=new Database();
			$str="insert into result(username,eid,score) values('".$uname."','".$eid."',".$this->score.")";
			$response=$db->query($str);
			echo "Result inserted".$response;
		}
		
		public function getEid()
		{
			$db=new Database();
			$str="select eid from exam where examdate=CURDATE() and avail=1";
			$arr=$db->selectQuery($str);
			return $arr[0];
		}

		public function check($id,$value)
		{
			$db=new Database();
			$str="select correctans from questionnaire where qid=$id";
			$arr=$db->selectQuery($str);
			if($value==$arr[0])
			{
				$this->score=$this->score+1;
				return true;
			}
			else
			{
				$this->wrongAns=$this->wrongAns+1;
				return false;
			}
		}
		
		public function generateReports()
		{
			$db=new Database();
			//$str=" select user.username,CONCAT(user.firstname,' ',user.lastname)name ,result.score,result.rank from user,result,exam where user.username=result.username and exam.avail=1";
			$str="select user.username,CONCAT(user.firstname,' ',user.lastname),result.score,result.rank from user,result where result.eid = (select eid from exam where avail=1)";
			$response=$db->reports($str);
			echo $response;
			
		}
	}
?>
